package cn.zhqr.core.interceptor;

import cn.zhqr.common.exception.SessionTimeoutException;
import cn.zhqr.common.bean.UserInfo;
import cn.zhqr.common.annotations.Anonymous;
import cn.zhqr.core.component.DefaultUserInfoManager;
import cn.zhqr.core.component.UserInfoHolder;
import cn.zhqr.core.component.UserInfoKit;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import org.apache.log4j.Logger;


/**
 * 登录拦截器
 * Created by charles on 2018/6/20.
 */
public class LoginInterceptor implements Interceptor{
    private static Logger logger = Logger.getLogger(LoginInterceptor.class);

    private static final String TOKEN_NAME = "x-token";

    /**
     * http请求参数名称,url参数或者post普通表单参数
     */
    private String paramName = TOKEN_NAME;
    /**
     * http请求头名称
     */
    private String headerName = TOKEN_NAME;
    /**
     * http cookie 名称
     */
    private String cookieName = TOKEN_NAME;

    public LoginInterceptor(){
        UserInfoKit.addManager(LoginInterceptor.class,new DefaultUserInfoManager());
    }

    @Override
    public void intercept(Invocation inv) {
        Controller controller = inv.getController();

        boolean allow_anonymous = controller.getClass().isAnnotationPresent(Anonymous.class)
                || inv.getMethod().isAnnotationPresent(Anonymous.class);

        String token = controller.getRequest().getHeader(getHeaderName());
        if(StrKit.isBlank(token)){
            token = controller.getPara(getParamName());
        }
        if(StrKit.isBlank(token)){
            token = controller.getCookie(getCookieName());
        }
        UserInfo userInfo = null;

        if(StrKit.notBlank(token)) {
            userInfo = getUserInfo(token);
        }
        if(userInfo==null && !allow_anonymous){
            throw new SessionTimeoutException();
        }

        if(userInfo!=null){
            //保存token和userInfo前执行的操作
            beforeSaveUserInfo(inv,token,userInfo);

            saveUserInfo(controller, token, userInfo);

            //保存token和userInfo后执行的操作
            afterSaveUserInfo(inv,token,userInfo);
        }

        inv.invoke();
    }

    public UserInfo getUserInfo(String token) {
        return UserInfoKit.getManager(LoginInterceptor.class).getUserInfo(token);
    }

    public void saveUserInfo(Controller controller, String token, UserInfo userInfo) {
        //保存token和userInfo
        UserInfoKit.getManager(LoginInterceptor.class).setUserInfo(token, userInfo);

        //将token和userInfo传递给Controller,方便Controller进行处理
        if(controller instanceof UserInfoHolder){
            ((UserInfoHolder) controller).setUserInfo(token, userInfo);
        }
    }


    /**
     * 在UserInfo存入缓存之前做一些处理
     * @param userInfo
     */
    public void beforeSaveUserInfo(Invocation inv, String token, UserInfo userInfo){

    }

    /**
     * 在UserInfo存入缓存之后做一些处理
     * @param userInfo
     */
    public void afterSaveUserInfo(Invocation inv, String token, UserInfo userInfo){

    }

    public String getParamName() {
        return paramName;
    }

    public void setParamName(String paramName) {
        this.paramName = paramName;
    }

    public String getHeaderName() {
        return headerName;
    }

    public void setHeaderName(String headerName) {
        this.headerName = headerName;
    }

    public String getCookieName() {
        return cookieName;
    }

    public void setCookieName(String cookieName) {
        this.cookieName = cookieName;
    }

}
